﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CE.Global;
using CE.HR;

public partial class HR_login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string _strCXLive = CE.Configuration.MyConfigurationManager.GetAppSettingValue("IsCXLive");
        if (_strCXLive == "0")
        {
            if (Session[CE.Global.General.Guest] == null)
            {
                Response.Redirect("~/Default2.aspx");
            }
        }

        if (Session[CE.Global.General.HRId] != null)
        {
            Response.Redirect("job-post.aspx");
        }
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        #region Existing Customer
        if (Page.IsValid)
        {
            AutoLogin();
        }
        #endregion
    }

    private void AutoLogin()
    {
        if (txtExEmail.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("AdminID") && txtExAuthCode.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("AdminPass"))
        {
            Session[CE.Global.General.HRTypeId] = "Admin";
            Response.Redirect("hr-list.aspx");
        }
        
        HRAccountDB _accountdb = new HRAccountDB();
        //Check Customer Account 
        HRAccount _account = _accountdb.GetUserAccount(txtExEmail.Text.ToLower(), 100);

        if (_account != null)
        {
            if (CheckUserLoginCached(_account.HRID.ToString()))
            {
                if (Server.HtmlDecode(txtExAuthCode.Text) == _account.AuthCode)
                {
                    if (_account.Status == true)
                    {
                        if (_account.Info1 == "0")
                        {
                            //Reset Email Fail Message
                            _account.Info1 = "";
                            _account.Info2 = "";
                            _accountdb.InsertUpdate(_account);
                        }

                        divExErrorMessage.Visible = false;
                        GetUserInformation(_account);
                        CE.Global.General.CreateUpdateHRLoginCached(Page);
                        _account = null;
                        Response.Redirect("job-post.aspx");
                    }
                    else
                    {
                        divExErrorMessage.Visible = true;
                        lblLoginMessage.Text = "Please verify your account by carefully following the instructions in the email. ";
                        lblLoginMessage.Text += "<br>Click <a href='resend-user-activate.aspx?email=" + _account.Email + "'>here</a> to resend the confirmation email.";
                    }
                }
                else
                {
                    divExErrorMessage.Visible = true;
                    lblLoginMessage.Text = "Email address or password is incorrect.";
                }
            }
            else
            {
                //Response.Redirect("~/User/UserMessage.aspx?mt=UM&ec=1993");
            }
        }
        else
        {
            divExErrorMessage.Visible = true;
            lblLoginMessage.Text = "Email address or password is incorrect.";
        }

        lblLoginMessage.ForeColor = System.Drawing.Color.White;
    }

    private bool CheckUserLoginCached(string UserId)
    {
        bool _retValue = false;
        string sKey = CE.Configuration.MyConfigurationManager.GetAppSettingValue("HRCacheName");
        sKey += UserId;
        if (HttpContext.Current.Cache[sKey] == null)
        {
            _retValue = true;
        }
        else
        {
            _retValue = false;
        }
        return _retValue;
    }

    private void GetUserInformation(HRAccount _account)
    {
        DateTime dtNow = new DateTime();
        dtNow = DateTime.Now;
        Session[CE.Global.General.HRId] = _account.HRID;
        Session[CE.Global.General.HRTypeId] = _account.HRTypeId;

        Session[CE.Global.General.HREmail] = _account.Email;
        Session[CE.Global.General.StartTime] = dtNow.ToString();
        
    }
}
